package com.china08.yxyapi.api.schwebsite.navbar;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 
 * 列表页-新闻资讯
 *
 */
@CrossOrigin
@RestController("SwsNavBarLeftArticleListController")
public class SwsNavBarLeftArticleListController extends YxyApiBaseController {

    @Autowired
    private SwsArticleRepository swsArticleRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/navbar/left/articles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String section2Id = reqModel.section2Id;
        String section1Id = reqModel.section1Id;
        String domainName = getDomainName(request);

        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");

        String schoolId = byDomainName.getSchoolId();
        Integer auditStatus = 1;
        List<SwsArticle> schoolIdAndSectionId = null;

        if ((StringUtils.isNotBlank(section1Id) && StringUtils.isBlank(section2Id))
                || (StringUtils.isNotBlank(section1Id) && StringUtils.equals(section2Id, "0"))) {
            schoolIdAndSectionId = swsArticleRepository
                    .findTop8ByOwnerSchoolIdAndAuditStatusAndSection1IdOrderByPvDesc(schoolId, auditStatus, section1Id);
        } else if (StringUtils.isNotBlank(section2Id)) {
            schoolIdAndSectionId = swsArticleRepository
                    .findTop8ByOwnerSchoolIdAndAuditStatusAndSection2IdOrderByPvDesc(schoolId, auditStatus, section2Id);
        } else {
            schoolIdAndSectionId = swsArticleRepository.findTop8ByOwnerSchoolIdAndAuditStatusOrderByPvDesc(schoolId,
                    auditStatus);
        }

        List<RespModel> list = new ArrayList<RespModel>();
        for (SwsArticle swsArticle : schoolIdAndSectionId) {
            RespModel respModel = new RespModel();
            respModel.title = swsArticle.getTitle();
            respModel.author = swsArticle.getAuthor();
            respModel.content = swsArticle.getContent();
            respModel.summary = swsArticle.getSummary();
            respModel.showDate = DateToolUtils.format(swsArticle.getShowDate(), "MM-dd");
            respModel.section1Id = swsArticle.getSection1Id();
            if (StringUtils.isBlank(swsArticle.getSection2Id())) {
                respModel.section2Id = "0";
            } else {
                respModel.section2Id = swsArticle.getSection2Id();
            }

            respModel.id = swsArticle.getId();
            list.add(respModel);
        }

        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String section2Id;

        public String section1Id;

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String title;

        public String summary;

        public String content;

        public String author;

        public String showDate;

        public String section2Id;

        public String section1Id;

    }

}
